package com.rimi.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.rimi.bean.MenuType;
import com.rimi.bean.Video;
import com.rimi.cache.SelectCache;
import com.rimi.dao.MenuTypeDao;
import com.rimi.dao.VideoDao;
import com.rimi.service.MenuTypeService;
import com.rimi.service.SelectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SelectServicelmpl extends ServiceImpl<VideoDao, Video> implements SelectService {

    @Autowired
    private MenuTypeDao menuTypeDao;

    @Autowired
    private MenuTypeService menuTypeService;

    @Autowired
    private SelectService selectService;

    @Autowired
    private SelectCache selectCache;

    @Override
    public List<Video> getVideoList(String videoId) {
        //每次获取数据，需要判断缓存里面是否有数据
        List<Video> videos = (List<Video>) selectCache.getCacheList("videos",Video.class);
        if(videos!=null && !videos.isEmpty()){
            return videos;
        }
        //Video video = (Video) selectService.selectObj(new EntityWrapper<Video>().eq("video_id", videoId));
        List<Video> list = selectService.selectList(new EntityWrapper<Video>().eq("video_id",videoId));
        selectCache.addCache("videos",list);
        return list;
    }

    @Override
    public List<String> getVideoTypeList(String videoId) {
        List<String> types = (List<String>) selectCache.getCacheList("videoTypes",String.class);
        if(types!=null && !types.isEmpty()){
            return types;
        }
        List list = menuTypeDao.getMenuTypeList(videoId);
        List<String> list2 = new ArrayList();
        for(int i=0 ; i<list.size() ; i++){
            MenuType menuType = menuTypeService.selectById(list.get(i)+"");
            list2.add(menuType.getMenuTypeName());
        }
        selectCache.addCache("videoTypes",list2);
        return list2;
    }


}
